package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constant.GlobalConstant;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.pojos.ApUserRealname;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dots.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.service.ApUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    @Autowired
    ApUserService apUserService;
    @Autowired
    KafkaTemplate kafkaTemplate;
    @Override
    public ResponseResult pageQuery(AuthDto dto) {
        if(dto==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        dto.checkParam();
        if (dto.getStatus()!=null&&!ApUserRealname.Status.check(dto.getStatus().shortValue()))
        {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<ApUserRealname> lqw=new LambdaQueryWrapper<>();
        if (dto.getStatus()!=null)
        {
            lqw.eq(ApUserRealname::getStatus,dto.getStatus().shortValue());
        }
//        lqw.eq(false,ApUserRealname::getStatus,dto.getStatus().shortValue());
        IPage page=new Page(dto.getPage(),dto.getSize());
        page = this.page(page, lqw);
        return ResponseResult.okResult(page);
    }

    @Override
    public ResponseResult authFail(AuthDto dto) {
        if (dto==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        if (dto.getId()==null)
        {
            throw new CustomException(AppHttpCodeEnum.ACCOUNT_NOT_NULL);
        }
        boolean b = this.update(
                Wrappers.<ApUserRealname>lambdaUpdate().eq(ApUserRealname::getId, dto.getId())
                        .set(ApUserRealname::getStatus, ApUserRealname.Status.AUDIT_FAILURE.getValue())
                        .set(ApUserRealname::getReason, dto.getMsg())
        );
        return b?ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
    }

    @Override
    public ResponseResult authPass(AuthDto dto) {
        if (dto==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        if (dto.getId()==null)
        {
            throw new CustomException(AppHttpCodeEnum.ACCOUNT_NOT_NULL);
        }
        
        boolean b = this.update(
                Wrappers.<ApUserRealname>lambdaUpdate().eq(ApUserRealname::getId, dto.getId())
                        .set(ApUserRealname::getStatus, ApUserRealname.Status.AUDIT_FAILURE.getValue())
                        .set(ApUserRealname::getReason, dto.getMsg())
        );
        ApUserRealname apUserRealname = this.getById(dto.getId());
        ApUser user = apUserService.getById(apUserRealname.getUserId());
        WmUser wmUser=new WmUser();
        BeanUtils.copyProperties(user,wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(user.getId());
        sendMessageToAddWmUser(wmUser);
        ApAuthor apAuthor = new ApAuthor();
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    private void sendMessageToAddWmUser(WmUser wmUser) {
        kafkaTemplate.send(GlobalConstant.ADD_WM_USER, JSON.toJSONString(wmUser));
    }
}
